package com.rxyb.trade.service.impl;

import com.rxyb.core.respone.R;
import com.rxyb.trade.gateway.dto.PayOrderReq;
import com.rxyb.trade.gateway.dto.PayOrderRsp;
import com.rxyb.trade.gateway.dto.RefundOrderReq;
import com.rxyb.trade.gateway.dto.RefundOrderRsp;
import com.rxyb.trade.service.TradeService;
import com.rxyb.trade.service.logic.YinShangLogic;
import com.rxyb.trade.strategy.Strategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * <p>
 * 支付渠道表 服务实现类
 * </p>
 *
 * @author DUYH
 * @since 2020-06-19
 */
@Service
public class TradeServiceImpl implements TradeService {

//    @Autowired
//    private YinShangLogic yinShangLogic;

    @Autowired
    private final Map<String, Strategy> strategyMap = new ConcurrentHashMap<>();

    public TradeServiceImpl(Map<String, Strategy> strategyMap) {
        this.strategyMap.clear();
        strategyMap.forEach((k, v) -> this.strategyMap.put(k, v));
    }

    @Override
    public R<PayOrderRsp> pay(PayOrderReq payOrderReq) {
        return strategyMap.get(payOrderReq.getUseType()).pay(payOrderReq);
    }

    @Override
    public R<RefundOrderRsp> refund(RefundOrderReq refundOrderReq) {
        return strategyMap.get(refundOrderReq.getUseType()).refund(refundOrderReq);
    }
}
